package com.zto.wk.webflux.user;

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.socket.WebSocketSession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by wangkuan at 2018-08-13 16:11
 */
@Component
public class UserManager {
    private static final Map<String, UserSession> SESSION_MAP = new ConcurrentHashMap<>();
    private static final Map<String, String> NAME_MAP = new ConcurrentHashMap<>();

    public void add(String key, WebSocketSession session, String name) {
        UserSession userSession = new UserSession(key, name, session);

        SESSION_MAP.put(key, userSession);
        NAME_MAP.put(key, name);
    }

    public void remove(String key) {
        SESSION_MAP.remove(key);
        NAME_MAP.remove(key);
    }

    public UserSession get(String key) {
        return SESSION_MAP.get(key);
    }

    public Map<String, String> getNameMap() {
        return NAME_MAP;
    }
}
